package sample.cics;

import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.Interaction;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.InteractionSpec;
import javax.resource.cci.Record;
import javax.resource.cci.ResourceAdapterMetaData;


/**
 * @j2c.connectionFactory jndi-name="MyDefaultJNDIName"
 * @j2c.managedConnectionFactory class="com.ibm.connector2.cics.ECIManagedConnectionFactory"
 * @j2c.managedConnectionFactory-property name="password" value="66666"
 * @j2c.managedConnectionFactory-property name="userName" value="QR030"
 * @j2c.managedConnectionFactory-property name="connectionURL" value="localhost"
 * @j2c.managedConnectionFactory-property name="serverName" value="SCSCPJA1"
 * @j2c.connectionSpec class="com.ibm.connector2.cics.ECIConnectionSpec"
 * @generated
 */
public class CustomerImpl implements sample.cics.Customer {

	private InteractionSpec invokedInteractionSpec;
	private InteractionSpec interactionSpec;
	private ConnectionSpec connectionSpec;
	private Connection connection;
	private ConnectionFactory connectionFactory;

	/**
	 * @j2c.interactionSpec class="com.ibm.connector2.cics.ECIInteractionSpec"
	 * @j2c.interactionSpec-property name="functionName" value="IBMTEST"
	 * @generated
	 */
	public sample.cics.data.CustomerInfo getCustomer(sample.cics.data.CustomerInfo arg) throws javax.resource.ResourceException {
		ConnectionSpec cs = getConnectionSpec();
		if (cs == null) {
			cs = new com.ibm.connector2.cics.ECIConnectionSpec();
		}
		InteractionSpec is = interactionSpec;
		if (is == null) {
			is = new com.ibm.connector2.cics.ECIInteractionSpec();
			((com.ibm.connector2.cics.ECIInteractionSpec) is).setFunctionName("IBMTEST");
		}
		sample.cics.data.CustomerInfo output = new sample.cics.data.CustomerInfo();
		invoke(cs, is, arg, output);
		return output;
	}

	/**
	 * @generated
	 */
	public ConnectionFactory getConnectionFactory() {
		return connectionFactory;
	}

	/**
	 * @generated
	 */
	public void setConnectionFactory(ConnectionFactory newConnectionFactory) {
		connectionFactory = newConnectionFactory;
		connection = null;
	}

	/**
	 * @generated
	 */
	public Connection getConnection() {
		return connection;
	}

	/**
	 * @generated
	 */
	public void setConnection(Connection newConnection) {
		connection = newConnection;
	}

	/**
	 * @generated
	 */
	public ConnectionSpec getConnectionSpec() {
		return connectionSpec;
	}

	/**
	 * @generated
	 */
	public void setConnectionSpec(ConnectionSpec newConnectionSpec) {
		connectionSpec = newConnectionSpec;
	}

	/**
	 * @generated
	 */
	public InteractionSpec getInteractionSpec() {
		return invokedInteractionSpec != null ? invokedInteractionSpec : interactionSpec;
	}

	/**
	 * @generated
	 */
	public void setInteractionSpec(InteractionSpec newInteractionSpec) {
		interactionSpec = newInteractionSpec;
	}

	/**
	 * @generated
	 */
	protected void invoke(ConnectionSpec cs, InteractionSpec is, Record arg, Record output) throws ResourceException {
		Connection conn = getConnection();
		ConnectionFactory cf = getConnectionFactory();
		boolean localConnection = conn == null;
		if (localConnection && cf != null) {
			if (cs != null)
				conn = cf.getConnection(cs);
			else
				conn = cf.getConnection();
		}
		if (conn != null) {
			Interaction interaction = conn.createInteraction();
			try {
				boolean executeInOut = true;
				if (cf != null) {
					ResourceAdapterMetaData raMD = cf.getMetaData();
					if (raMD != null)
						executeInOut = raMD.supportsExecuteWithInputAndOutputRecord();
				}
				if (executeInOut)
					interaction.execute(is, arg, output);
				else
					output = interaction.execute(is, arg);
			} finally {
				interaction.close();
				if (localConnection)
					conn.close();
				invokedInteractionSpec = is;
			}
		}
		return;
	}

	/**
	 * @generated
	 */
	protected void initializeBinding() throws ResourceException {
		ConnectionFactory cf = null;
		String jndiName = "MyDefaultJNDIName";
		javax.naming.Context ctx = null;
		try {
			ctx = new javax.naming.InitialContext();
			cf = (ConnectionFactory) ctx.lookup("java:comp/env/" + jndiName);
			if (cf == null)
				throw new javax.naming.NamingException();
		} catch (javax.naming.NamingException exception) {
			try {
				if (ctx == null)
					throw exception;
				cf = (ConnectionFactory) ctx.lookup(jndiName);
				if (cf == null)
					throw new javax.naming.NamingException();
			} catch (javax.naming.NamingException exception1) {
				com.ibm.connector2.cics.ECIManagedConnectionFactory mcf = new com.ibm.connector2.cics.ECIManagedConnectionFactory();
				mcf.setPassword("66666");
				mcf.setUserName("QR030");
				mcf.setConnectionURL("localhost");
				mcf.setServerName("SCSCPJA1");
				cf = (ConnectionFactory) mcf.createConnectionFactory();
			}
		}
		setConnectionFactory(cf);
		ConnectionSpec cs = new com.ibm.connector2.cics.ECIConnectionSpec();
		setConnectionSpec(cs);
	}

	/**
	 * @generated
	 */
	public CustomerImpl() throws ResourceException {
		initializeBinding();
	}
}